//
//  homeViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/10.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "homeViewController.h"
#import "UIView+Common.h"
#import "lunboTableViewCell.h"
#import "kaituanTableViewCell.h"
#import "shangxinTableViewCell.h"
#import "remaiTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "goodsViewController.h"
#import "KaiTuanOneTableViewCell.h"
#import "HomeABargainViewController.h"
#import "MaiTianLogViewController.h"
#import "detailsViewController.h"
#import "Masonry.h"
#import "IntegralMallViewController.h"
#import "cooperatingPartnerViewController.h"
#import "configddd.h"
#import "searchViewController.h"
#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
#import <CoreLocation/CoreLocation.h>



@interface homeViewController ()<CLLocationManagerDelegate>{
    
    //    CLLocationManager * _locationManager;
    NSString * currentCity; //当前城市
    CLGeocoder *_geocoder;
}

@property(nonatomic,strong) CLLocationManager *locationManager;

@property(nonatomic,strong) UIScrollView *sxssScrollView;

@property(nonatomic,strong) UIScrollView *sxDDDScrollView;

@property(nonatomic,strong) UIScrollView *todayNewScrollView;

@end

@implementation homeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
    
    [super viewWillAppear:YES];


    czTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self creationData];
        
        [czTableView.mj_header beginRefreshing];
    }];
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
     [self creationData];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationNotification:) name:@"location" object:nil];
    [self setTitle:@"LEMAI CIRCLE"];
    [self leftNavHide];
    kaituan=1;
    
    
    
    UIButton *leftButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (62), (22))];
    //    leftButton.backgroundColor=[UIColor redColor];
    [leftButton addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *addressImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, (22), (22))];
    addressImageView.image=[UIImage imageNamed:@"address"];
    [leftButton addSubview:addressImageView];
    
    addressLabel=[[UILabel alloc]initWithFrame:CGRectMake((22), (3), (100), (16))];
    addressLabel.text=@"北京市";
    addressLabel.textColor=[UIColor whiteColor];
    [leftButton addSubview:addressLabel];
    UIBarButtonItem *leftBarButton=[[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem=leftBarButton;
    
    UIButton *rightButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (22), (22))];
    [rightButton addTarget:self action:@selector(rightNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"serach"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"serach"] forState:UIControlStateSelected];
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem=rightBarButton;
    
    _topNaviV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (42))];
    _topNaviV.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_topNaviV];
    
    _scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _topNaviV.bottom, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(1))];
    [_scrollV setPagingEnabled:YES];//开启分页，也就是不允许滑动到一半
    [_scrollV setShowsHorizontalScrollIndicator:NO];//设置水平方向的滚动条:
    [self.view addSubview:_scrollV];
    _scrollV.delegate = self;
    _scrollV.bounces=NO;
    _scrollV.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    [_scrollV.panGestureRecognizer addTarget:self action:@selector(scrollHandlePan:)];
    
    //    [self createTwo];
    
    UIView *xView=[[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-49-64-(1), DEF_SCREEN_WIDTH, (1))];
    xView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:xView];
    
    [self createTwo];
    
}
- (void)creationData{

    
    [[MNetworkHandler sharedInstance]conURL:Home_Main
                                networkType:NetWorkPOST
                                     params:nil
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       //                                       [PublicFunction saveConfig:userTextField.text forKey:@"phone"];
                                       //                                       [PublicFunction saveConfig:[returnData objectForKey:@"sessionID"] forKey:@"sessionID"];
                                       
                                       //                                       myArr = [returnData objectForKey:@"data"];
                                       carouselArr=[returnData objectForKey:@"carousel"];
                                       shopteamDict=[returnData objectForKey:@"shopteam"];
                                       tenArr=[shopteamDict objectForKey:@"ten"];
                                       fourteenArr=[shopteamDict objectForKey:@"ten"];
                                       
                                       twentyArr=[shopteamDict objectForKey:@"twenty"];
                                       newshopArr=[returnData objectForKey:@"newshop"];
                                       hotshopArr=[returnData objectForKey:@"hotshop"];
                                       NSLog(@"%@", hotshopArr);
                                       
                                       
                                       [czTableView reloadData];
                                       [czTableView.mj_header endRefreshing];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"222222222222222222%@",error);
                                   
                                   
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];

    
    
    
}
- (void)createTwo
{
    NSArray *arT = @[@"首页", @"超值自营", @"合作商家", @"积分抽奖", @"麦田日志"];
    _navScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (42))];
    [_navScrollV setShowsHorizontalScrollIndicator:NO];
    for (int i = 0; i < [arT count]; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font=[UIFont systemFontOfSize:(16)];
        [btn setFrame:CGRectMake((80) * i, (13), (80), (14))];
        
        [btn setTitleColor:[ZJCModel colorWithString:@"222222"] forState:UIControlStateSelected];
        [btn setTitleColor:[ZJCModel colorWithString:@"666666"] forState:UIControlStateNormal];
        [btn setTitle:[arT objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(actionbtn:) forControlEvents:UIControlEventTouchUpInside];
        [_navScrollV addSubview:btn];
    }
    [_navScrollV setContentSize:CGSizeMake((80) * [arT count], (14))];
    [_topNaviV addSubview:_navScrollV];
    
    // 黑线
    _navBgV = [[UIView alloc] initWithFrame:CGRectMake((10), (40) ,(60), (2))];
    [_navBgV setBackgroundColor:[ZJCModel colorWithString:@"222222"]];
    [_navScrollV addSubview:_navBgV];
    
    // 灰线
    UIView *gView=[[UIView alloc]initWithFrame:CGRectMake(0, (41), (80) * [arT count], (1))];
    gView.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    [_navScrollV addSubview:gView];
    
    [_scrollV setContentSize:CGSizeMake(_scrollV.frame.size.width * [arT count], _scrollV.frame.size.height)];
    
    
    
    czTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(1)) style:UITableViewStylePlain];
    czTableView.delegate=self;
    czTableView.dataSource=self;
    czTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_scrollV addSubview:czTableView];
    
    
    HomeABargainViewController *aaaa = [[HomeABargainViewController alloc] init];
    aaaa.view.frame = CGRectMake(DEF_SCREEN_WIDTH, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(1));
    [self addChildViewController:aaaa];
    
    
    [_scrollV addSubview:aaaa.view];
    
    MaiTianLogViewController *maitian = [[MaiTianLogViewController alloc] init];
    maitian.view.frame = CGRectMake(DEF_SCREEN_WIDTH * 4, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(1));
    [self addChildViewController:maitian];
    [_scrollV addSubview:maitian.view];
    

    IntegralMallViewController *integralMall = [[IntegralMallViewController alloc] init];
    integralMall.view.frame =CGRectMake(DEF_SCREEN_WIDTH * 3, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(1));
    [self addChildViewController:integralMall];
    [_scrollV addSubview:integralMall.view];
    
    cooperatingPartnerViewController *cooperatingPartnerVC = [[cooperatingPartnerViewController alloc]init];
    cooperatingPartnerVC.view.frame = CGRectMake(DEF_SCREEN_WIDTH * 2, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 49 - screenScale(80));
    [self addChildViewController:cooperatingPartnerVC];
    [_scrollV addSubview:cooperatingPartnerVC.view];
    
    
}

#pragma mark ---------------------------------- NSNotification

- (void)locationNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"select"]isEqualToString:@"yes"]) {
        
        addressLabel.text = text.userInfo[@"location"];
    }
}

#pragma mark - action
- (void)actionbtn:(UIButton *)btn
{
    wareIndex = btn.tag-1;
    [_scrollV scrollRectToVisible:CGRectMake(_scrollV.frame.size.width * (btn.tag - 1), _scrollV.frame.origin.y, _scrollV.frame.size.width, _scrollV.frame.size.height) animated:YES];
    
    float xx = _scrollV.frame.size.width * (btn.tag - 1) * ((80) / self.view.frame.size.width) - (80);
    [_navScrollV scrollRectToVisible:CGRectMake(xx, 0, _navScrollV.frame.size.width, _navScrollV.frame.size.height) animated:YES];
}


// 两边显示左右页面
-(void)scrollHandlePan:(UIPanGestureRecognizer*) panParam
{
    BOOL isPaning = NO;
    //NSLog(@"fff%f",_scrollV.contentOffset.x);
    if(_scrollV.contentOffset.x <= 0)
    {
        isPaning = YES;
        //[self.sideMenuViewController presentLeftMenuViewController];
    }
    else if(_scrollV.contentOffset.x >= (_scrollV.contentSize.width - _scrollV.frame.size.width))
    {
        isPaning = YES;
        //[self.sideMenuViewController presentRightMenuViewController];
    }
    if(isPaning)
    {
        [self.sideMenuViewController panGestureRecognizedFrom:panParam];
    }
}

// 改变红线位置
- (void)changeView:(float)x
{
    float xx = x * ((80) / self.view.frame.size.width);
    
    [_navBgV setFrame:CGRectMake(xx+(10), _navBgV.frame.origin.y, _navBgV.frame.size.width, _navBgV.frame.size.height)];
}
#pragma mark - UIScrollViewDelegate
// 开始拖拽视图
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //scrollView.contentOffset.x;
    NSLog(@"scrollViewWillBeginDragging");
}
// 只要滚动了就会触发
//去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 改变红线
    [self changeView:scrollView.contentOffset.x];
    
    CGFloat sectionHeaderHeight = (108);//czTableView.sectionHeaderHeight;
    CGFloat sectionFooterHeight = (92);//czTableView.sectionFooterHeight;
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(scrollView.contentSize.height - scrollView.frame.size.height - sectionFooterHeight), 0);
    }
}
//完成拖拽
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    wareIndex= scrollView.contentOffset.x/DEF_SCREEN_WIDTH;
    // 滚动到红线对应菜单
    float xx = scrollView.contentOffset.x * ((80) / self.view.frame.size.width) - (80);
    [_navScrollV scrollRectToVisible:CGRectMake(xx, 0, _navScrollV.frame.size.width, _navScrollV.frame.size.height) animated:YES];
}

#pragma mark - 点击左导航
-(void)leftNavItemClick{
    NSLog(@"点击定位");
    
    [self locate];
}

#pragma mark - 点击右导航
-(void)rightNavItemClick{
    NSLog(@"搜索");
    
    searchViewController *searchVC = [[searchViewController alloc]init];
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -----UITableViewDataSource-----

//返回分区数(默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

//返回每一行的cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        if (kaituan==1) {
            return 1;
        }else if(kaituan==2){
            return 1;
        }
        return 0;
    }else if (section == 2){
    
        return 1;
    }else if (section == 0){
    
        return 1;
        
    }else {
    
        return  1;
        
    }
    
}

//自定义section的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==2) {
        UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (98+10))];
        xView.backgroundColor=[ZJCModel colorWithString:@"222222"];
        //add your code behind
        UILabel *xixnLabel=[[UILabel alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(80))/2, (38), (80), (15))];
        xixnLabel.text=@"今日新品";
        xixnLabel.font=[UIFont boldSystemFontOfSize:(18)];
        xixnLabel.textColor=[UIColor whiteColor];
        xixnLabel.textAlignment=NSTextAlignmentCenter;
        [xView addSubview:xixnLabel];
        UILabel *xinLabel1=[[UILabel alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(80))/2, (38+25), (80), (10))];
        xinLabel1.text=@"10点上新";
        xinLabel1.textColor=[UIColor grayColor];
        xinLabel1.font=[UIFont systemFontOfSize:(14)];
        xinLabel1.textAlignment=NSTextAlignmentCenter;
        [xView addSubview:xinLabel1];
        return xView;
    }else if (section==3){
        UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (85))];
        xView.backgroundColor=[UIColor whiteColor];
        //add your code behind
        UILabel *xiinLabel=[[UILabel alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(80))/2, (35), (80), (15))];
        xiinLabel.text=@"超值热卖";
        xiinLabel.font=[UIFont boldSystemFontOfSize:(18)];
        xiinLabel.textAlignment=NSTextAlignmentCenter;
        [xView addSubview:xiinLabel];
        return xView;
    }else if ((section==1&&twentyArr.count)||(section==1&&fourteenArr.count)){
        UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (100))];
        xView.backgroundColor=[UIColor whiteColor];
        //add your code behind
        
        tenButton=[[UIButton alloc]initWithFrame:CGRectMake((85), (40), (100), (25))];
        [tenButton setTitle:@"14点开团" forState:UIControlStateNormal];
        [tenButton addTarget:self action:@selector(tenButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        tenButton.titleLabel.font=[UIFont boldSystemFontOfSize:(18)];
        [tenButton setTitleColor:[ZJCModel colorWithString:@"222222"] forState:UIControlStateNormal];
        tenButton.tag=1;
        [xView addSubview:tenButton];
        xxView=[[UIView alloc]initWithFrame:CGRectMake((15), (23), (70), (2))];
        xxView.backgroundColor=[ZJCModel colorWithString:@"222222"];
        [tenButton addSubview:xxView];
        
        eButton=[[UIButton alloc]initWithFrame:CGRectMake((190+5), (40), (100), (25))];
        [eButton setTitle:@"晚8疯抢" forState:UIControlStateNormal];
        [eButton addTarget:self action:@selector(tenButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        eButton.titleLabel.font=[UIFont boldSystemFontOfSize:(15)];
        [eButton setTitleColor:[ZJCModel colorWithString:@"666666"] forState:UIControlStateNormal];
        eButton.tag=2;
        [xView addSubview:eButton];
        xxxView=[[UIView alloc]initWithFrame:CGRectMake((15), (23), (70), (2))];
        xxxView.backgroundColor=[ZJCModel colorWithString:@"222222"];
        xxxView.hidden=YES;
        [eButton addSubview:xxxView];
        
        if (kaituan==1) {
            xxView.hidden=NO;
            xxxView.hidden=YES;
            tenButton.titleLabel.font=[UIFont boldSystemFontOfSize:(18)];
            [tenButton setTitleColor:[ZJCModel colorWithString:@"222222"] forState:UIControlStateNormal];
            eButton.titleLabel.font=[UIFont boldSystemFontOfSize:(15)];
            [eButton setTitleColor:[ZJCModel colorWithString:@"666666"] forState:UIControlStateNormal];
           
        }else if (kaituan==2){
            xxxView.hidden=NO;
            xxView.hidden=YES;
            eButton.titleLabel.font=[UIFont boldSystemFontOfSize:(18)];
            [eButton setTitleColor:[ZJCModel colorWithString:@"222222"] forState:UIControlStateNormal];
            tenButton.titleLabel.font=[UIFont boldSystemFontOfSize:(15)];
            [tenButton setTitleColor:[ZJCModel colorWithString:@"666666"] forState:UIControlStateNormal];
            
        }
        return xView;
    }
    UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (0))];
    return xView;
}
//自定义section头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==2) {
        return (98+10);
    }else if (section==3){
        return (85);
    }else if (section==1&&twentyArr!=nil){
        return (100);
    }else if (section==1&&fourteenArr.count!=0){
        return (100);
    }
    return 0;
}

////自定义section的尾部
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    if (section==2) {
//        UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (92))];
//        xView.backgroundColor=[ZJCModel colorWithString:@"222222"];
//        //add your code behind
//        UIButton *xButton=[[UIButton alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(72))/2, (34), (72), (21))];
//        xButton.layer.borderWidth=(1);
//        xButton.layer.borderColor=[UIColor grayColor].CGColor;
//        [xButton setTitle:@"更多" forState:UIControlStateNormal];
//        [xButton addTarget:self action:@selector(xButtonClick) forControlEvents:UIControlEventTouchUpInside];
//        [xView addSubview:xButton];
//        return xView;
//    }
//    UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (0))];
//    return xView;
//}
////自定义section尾部的高度
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    if (section==2) {
//        return (92);
//    }
//    return 0;
//}


#pragma mark -----UITableViewDelegate-----
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return (200);
    }else if (indexPath.section==1){
        return (319);
    }else if (indexPath.section==2){
        return (204+14);
    }
    return (305*hotshopArr.count);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        lunboTableViewCell *cell=(lunboTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"lunboTableViewCell"];
        if (cell == nil) {
            cell=[[lunboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"lunboTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (carouselArr) {
            
            [cell updateInfo:carouselArr];
            
        }
        return cell;
    }else if (indexPath.section==1){
        //开团

        if (kaituan==1) {//14点开团
            KaiTuanOneTableViewCell *cell=(KaiTuanOneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"shangxinTableViewCell"];
            if (cell == nil) {
                cell=[[KaiTuanOneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"shangxinTableViewCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            self.sxssScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (204+14))];
            self.sxssScrollView.contentSize=CGSizeMake(DEF_SCREEN_WIDTH*(fourteenArr.count),(204+14));
            
            self.sxssScrollView.backgroundColor=[UIColor whiteColor];
            self.sxssScrollView.showsHorizontalScrollIndicator=NO;
            self.sxssScrollView.pagingEnabled = YES;
            [cell addSubview:self.sxssScrollView];
            
            
            
           // [self.sxssScrollView removeAllSubviews];
            
            

            

            for (int i=0; i<fourteenArr.count; i++) {
                UIButton *BTN=[[UIButton alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH)*i, 0, DEF_SCREEN_WIDTH, (204+14))];
                
                
                BTN.tag = [[fourteenArr[i] objectForKey:@"shop_id"] integerValue];
                
                [BTN addTarget:self action:@selector(myBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                
              //  [cell.sxScrollView addSubview:BTN];
                [self.sxssScrollView addSubview:BTN];
                dict1=fourteenArr[i];
                
                
                
                NSString *urlWare=[dict1 objectForKey:@"shoppic"];
                NSString *urlN=manage(AP_HOST, urlWare);
                NSLog(@"%@",urlN);
                NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
                NSURL *urlW=[NSURL URLWithString:strUrl];
                
                
                UIImageView *picImageView=[[UIImageView alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH / 2 - 75, 0,(150), (150))];
                
                [picImageView sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
                [BTN addSubview:picImageView];
                
                
                UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH / 2 - 75, (150+12), (150), (30))];
                nameLabel.numberOfLines=0;
                nameLabel.font=[UIFont systemFontOfSize:(15)];
                nameLabel.textColor = [UIColor blackColor];
                nameLabel.text=[dict1 objectForKey:@"shop_name"];
                nameLabel.textAlignment = NSTextAlignmentCenter;
                
                [BTN addSubview:nameLabel];
                
                UILabel *priceLabel=[[UILabel alloc] init];
                
                NSString *strPic = @"¥";
                priceLabel.text= [strPic stringByAppendingString:[dict1 objectForKey:@"price"]];
                priceLabel.textColor=[UIColor whiteColor];
                priceLabel.font=[UIFont systemFontOfSize:(15)];
                priceLabel.textColor = [UIColor blackColor];
                [BTN addSubview:priceLabel];
                
                [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(nameLabel.mas_bottom).with.offset(10);
                    make.left.equalTo(BTN.mas_centerX);
                    make.height.offset(17);
                    make.width.offset(100);
                    
                }];
                
                UILabel *labelOfFK = [[UILabel alloc] init];
                [BTN addSubview:labelOfFK];
                labelOfFK.backgroundColor = [UIColor blackColor];
                labelOfFK.textColor = [UIColor whiteColor];
                labelOfFK.text = @"疯抢价";
                labelOfFK.font = [UIFont systemFontOfSize:15];
                labelOfFK.textAlignment = NSTextAlignmentRight;
                [labelOfFK mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(nameLabel.mas_bottom).with.offset(10);
                    make.right.equalTo(BTN.mas_centerX);
                    make.height.offset(17);
                   
                    
                }];
                
                
            }
            return cell;

            
            
            
        }else if (kaituan==2){
            //晚8点开团
                kaituanTableViewCell *cell=(kaituanTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"kaituanTableViewCell"];
                if (cell == nil) {
                    cell=[[kaituanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kaituanTableViewCell"];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
            self.sxDDDScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (204+14))];
            self.sxDDDScrollView.contentSize=CGSizeMake(DEF_SCREEN_WIDTH*(twentyArr.count),(204+14));
            
            self.sxDDDScrollView.backgroundColor=[UIColor whiteColor];
            self.sxDDDScrollView.showsHorizontalScrollIndicator=NO;
            self.sxDDDScrollView.pagingEnabled = YES;
            [cell addSubview:self.sxDDDScrollView];
            
            
                //[cell.sxScrollView removeAllSubviews];
            
            
            for (int i=0; i<twentyArr.count; i++) {
                UIButton *BTN=[[UIButton alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH)*i, 0, DEF_SCREEN_WIDTH, (204+14))];
                
                
                BTN.tag = [[twentyArr[i] objectForKey:@"shop_id"] integerValue];
                
                [BTN addTarget:self action:@selector(myBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                
               // [cell.sxScrollView addSubview:BTN];
                [self.sxDDDScrollView addSubview:BTN];
                dict1=twentyArr[i];
                
                
                NSString *urlWare=[dict1 objectForKey:@"shoppic"];
                NSString *urlN=manage(AP_HOST, urlWare);
                NSLog(@"%@",urlN);
                NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
                NSURL *urlW=[NSURL URLWithString:strUrl];
                
                
                UIImageView *picImageView=[[UIImageView alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH / 2 - 75, 0,(150), (150))];
                
                [picImageView sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
                [BTN addSubview:picImageView];
                
                
                UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH / 2 - 75, (150+12), (150), (30))];
                nameLabel.numberOfLines=0;
                nameLabel.font=[UIFont systemFontOfSize:(15)];
                nameLabel.textColor = [UIColor blackColor];
                nameLabel.text=[dict1 objectForKey:@"shop_name"];
                nameLabel.textAlignment = NSTextAlignmentCenter;
                
                [BTN addSubview:nameLabel];
                
                UILabel *priceLabel=[[UILabel alloc] init];
                
                NSString *strPic = @"¥";
                priceLabel.text= [strPic stringByAppendingString:[dict1 objectForKey:@"price"]];
                priceLabel.textColor=[UIColor whiteColor];
                priceLabel.font=[UIFont systemFontOfSize:(15)];
                priceLabel.textColor = [UIColor blackColor];
                [BTN addSubview:priceLabel];
                
                [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(nameLabel.mas_bottom).with.offset(10);
                    make.left.equalTo(BTN.mas_centerX);
                    make.height.offset(17);
                    make.width.offset(100);
                    
                }];
                
                UILabel *labelOfFK = [[UILabel alloc] init];
                [BTN addSubview:labelOfFK];
                labelOfFK.backgroundColor = [UIColor blackColor];
                labelOfFK.textColor = [UIColor whiteColor];
                labelOfFK.text = @"疯抢价";
                labelOfFK.font = [UIFont systemFontOfSize:15];
                labelOfFK.textAlignment = NSTextAlignmentRight;
                [labelOfFK mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(nameLabel.mas_bottom).with.offset(10);
                    make.right.equalTo(BTN.mas_centerX);
                    make.height.offset(17);
                    
                    
                }];
                
            }
            return cell;

 
        
        }
    }//今天上新
        else if (indexPath.section==2){
        shangxinTableViewCell *cell=(shangxinTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"shangxinTableViewCell"];
        if (cell == nil) {
            cell=[[shangxinTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"shangxinTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        self.todayNewScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, (204+14))];
        self.todayNewScrollView.contentSize=CGSizeMake(160*(newshopArr.count)+(10),(204+14));
        self.todayNewScrollView.backgroundColor=[ZJCModel colorWithString:@"222222"];
            //        self.sxScrollView.showsHorizontalScrollIndicator=NO;
        [cell addSubview:self.todayNewScrollView];
            
        [cell.sxScrollView removeAllSubviews];
        for (int i=0; i<newshopArr.count; i++) {
            UIButton *BTN=[[UIButton alloc]initWithFrame:CGRectMake((150)*i+(10)*(i+1), 0, (150), (204+14))];
     
            
            BTN.tag = [[newshopArr[i] objectForKey:@"shop_id"] integerValue];
            
            [BTN addTarget:self action:@selector(myBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
    //        [cell.sxScrollView addSubview:BTN];
            [self.todayNewScrollView addSubview:BTN];
            dict1=newshopArr[i];
            
            
            NSString *urlWare=[dict1 objectForKey:@"shoppic"];
            NSString *urlN=manage(AP_HOST, urlWare);
            NSLog(@"%@",urlN);
            NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
            NSURL *urlW=[NSURL URLWithString:strUrl];

            
            UIImageView *picImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,(150), (150))];

            [picImageView sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
            [BTN addSubview:picImageView];
            
            UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, (150+12), (150), (30))];
            nameLabel.numberOfLines=0;
            nameLabel.font=[UIFont systemFontOfSize:(12)];
            nameLabel.textColor=[UIColor whiteColor];
            nameLabel.text=[dict1 objectForKey:@"shop_name"];
            [BTN addSubview:nameLabel];
            
            UILabel *priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, (150+24+30), (100), (14))];
            //            priceLabel.text=@"￥72";
            priceLabel.text=[dict1 objectForKey:@"price"];
            priceLabel.textColor=[UIColor whiteColor];
            priceLabel.font=[UIFont systemFontOfSize:(12)];
            [BTN addSubview:priceLabel];
        }
        return cell;
        }
//超值热卖
    remaiTableViewCell *cell=(remaiTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"remaiTableViewCell"];
    if (cell == nil) {
        cell=[[remaiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"remaiTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.VC = self;
    cell.num = [NSString stringWithFormat:@"%d", hotshopArr.count];
    [cell updateInfo:hotshopArr];
    //    cell.settingVC=self;//控制器传给cell
    return cell;
}

-(void)myBtnClick:(UIButton*)sender{
    
    detailsViewController *detailsVC=[[detailsViewController alloc]init];
    detailsVC.hidesBottomBarWhenPushed=YES;

//    NSMutableDictionary *dict = newshopArr[0];
    
//    detailsVC.myStr = [dict objectForKey:@"shop_id"];
    detailsVC.myStr = [NSString stringWithFormat:@"%ld", (long)sender.tag];
    
    
    
    
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}


-(void)tenButtonClick:(UIButton *)sender{
    if (sender.tag==1) {
        NSLog(@"14点开抢");
        kaituan=1;
        [czTableView reloadData];
    }else if (sender.tag==2){
        NSLog(@"晚8疯抢");
        kaituan=2;
        [czTableView reloadData];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark --------- 定位 ----------

- (void)locate {
    
    //定位管理器
    _locationManager=[[CLLocationManager alloc]init];
    //    _locationManager.allowsBackgroundLocationUpdates  = YES;
    //设置代理
    _locationManager.delegate = self;
    //设置定位精度
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    //定位频率,每隔多少米定位一次
    CLLocationDistance distance=10.0;//十米定位一次
    _locationManager.distanceFilter=distance;
    //启动跟踪定位
    [_locationManager startUpdatingLocation];
    
    _geocoder=[[CLGeocoder alloc]init];
    //    [self getCoordinateByAddress:@"北京"];
    //    [self getAddressByLatitude:39.54 longitude:116.28];
    
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前可能尚未打开，请设置打开！");
        return;
    }
    
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0) {
        
        [self.locationManager requestAlwaysAuthorization];
        [_locationManager requestWhenInUseAuthorization];
    }
    
    //如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        
        [_locationManager requestWhenInUseAuthorization];
        
    }else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
        
        //设置代理
        _locationManager.delegate = self;
        //设置定位精度
        _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        //定位频率,每隔多少米定位一次
        CLLocationDistance distance=10.0;//十米定位一次
        _locationManager.distanceFilter=distance;
        //启动跟踪定位
        [_locationManager startUpdatingLocation];
    }
    
}

#pragma mark CoreLocation delegate

//定位失败则执行此代理方法
//定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开定位设置
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:ok];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}
//定位成功
#pragma mark - CoreLocation 代理
#pragma mark 跟踪定位代理方法，每次位置发生变化即会执行（只要定位到相应位置）

//可以通过模拟器设置一个虚拟位置，否则在模拟器中无法调用此方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    CLLocation *location=[locations firstObject];//取出第一个位置
    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标
    NSLog(@"经度：%f,纬度：%f,海拔：%f,航向：%f,行走速度：%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
    //如果不需要实时定位，使用完即使关闭定位服务
    [_locationManager stopUpdatingLocation];
    
    //[self getAddressByLatitude:coordinate.latitude longitude:coordinate.longitude];
    
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             
             //将获得的所有信息显示到label上
             //self.location.text = placemark.name;
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
                 city = placemark.administrativeArea;
             }
             NSLog(@"city = %@", city);
             
                          addressLabel.text = placemark.administrativeArea;
             //             addressLabel.text = city;
             
             NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"yes",@"select",city,@"location", nil];
             NSNotification *notification =[NSNotification notificationWithName:@"location" object:nil userInfo:dict];
             [[NSNotificationCenter defaultCenter] postNotification:notification];
             
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
}

@end
